package com.sunlong.jiangwei.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.sunlong.jiangwei.dao.AsideMenuMapper;
import com.sunlong.jiangwei.dao.RoleMapper;
import com.sunlong.jiangwei.entity.AsideMenu;
import com.sunlong.jiangwei.entity.JwRole;
import com.sunlong.jiangwei.entity.JwUserRole;
import com.sunlong.jiangwei.service.IRoleService;
import com.sunlong.jiangwei.util.ErrorMapUtils;
import com.sunlong.jiangwei.vo.AsideMenuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RoleServiceImpl implements IRoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private AsideMenuMapper asideMenuMapper;

    @Override
    public Map<String, String> add(Map<String, Object> map) {
        String roleName = (String) map.get("roleName");
        String roleDes = (String) map.get("roleDes");
        List<String> powerList = (List<String>) map.get("powerList");
        Object powerListOptions = map.get("powerListOptions");
        String powerListJson = JSONArray.toJSONString(powerList);
        JwRole jwRole = new JwRole();
        jwRole.setRoleName(roleName);
        jwRole.setRoleDes(roleDes);
        jwRole.setPowerList(powerListJson);
        jwRole.setPowerListOptions(JSONArray.toJSONString(powerListOptions));
        roleMapper.insert(jwRole);
        return null;
    }

    public List<AsideMenuVo> findByPowerListString(String content) {
        List<String> stringList = JSONArray.parseArray(content, String.class);
        ArrayList<AsideMenuVo> asideMenuVos = new ArrayList<>();
        if (null == stringList) {
            return null;
        }
        for (String s : stringList) {
            String[] split = s.split("-");
            // 查询出父菜单
            AsideMenu asideMenu = asideMenuMapper.findById(Integer.parseInt(split[0]));
            AsideMenu sonAsideMenu = asideMenuMapper.findById(Integer.parseInt(split[1]));
            Boolean aBoolean = existAsideMenu(asideMenu, asideMenuVos);
            if (aBoolean){
                asideMenuVos = addToFaAsideMenu(asideMenu.getId(),sonAsideMenu,asideMenuVos);
            }else {
                // 添加父
                AsideMenuVo asideMenuVo = new AsideMenuVo();
                asideMenuVo.setValue(asideMenu.getId());
                asideMenuVo.setLabel(asideMenu.getAuthName());
                List<AsideMenuVo> children = null == asideMenuVo.getChildren() ? new ArrayList<>() : asideMenuVo.getChildren();
                // 添加子
                AsideMenuVo sonAsideMenuVo = new AsideMenuVo();
                sonAsideMenuVo.setValue(sonAsideMenu.getId());
                sonAsideMenuVo.setLabel(sonAsideMenu.getAuthName());
                children.add(sonAsideMenuVo);
                asideMenuVo.setChildren(children);
                asideMenuVos.add(asideMenuVo);
            }
        }
        return asideMenuVos;
    }

    private ArrayList<AsideMenuVo> addToFaAsideMenu(Integer id,AsideMenu sonAsideMenu, ArrayList<AsideMenuVo> asideMenuVos) {
        for (AsideMenuVo asideMenuVo : asideMenuVos) {
            if (asideMenuVo.getValue() == id){
                AsideMenuVo sonAsideMenuVo = new AsideMenuVo();
                sonAsideMenuVo.setValue(sonAsideMenu.getId());
                sonAsideMenuVo.setLabel(sonAsideMenu.getAuthName());
                List<AsideMenuVo> children = asideMenuVo.getChildren();
                if (null == children || children.size() == 0){
                    children = new ArrayList<>();
                }
                children.add(sonAsideMenuVo);
                asideMenuVo.setChildren(children);
                System.out.println("-----------"+asideMenuVo);
            }
        }
        return asideMenuVos;
    }

    public Boolean existAsideMenu(AsideMenu asideMenu, List<AsideMenuVo> asideMenuVos){
        Boolean flag = false;
        for (AsideMenuVo asideMenuVo : asideMenuVos) {
            if (asideMenuVo.getValue() == asideMenu.getId()){
                flag = true;
            }
        }
        System.out.println("-----------"+flag);
        return flag;
    }

    @Override
    @Transactional
    public Map<String, String> delete(Map<String, Object> map) {
        Long id = ((Integer)map.get("id")).longValue();
        JwRole role = roleMapper.selectRoleById(id);
        HashMap<String, String> errorMap = new HashMap<>();
        if (null == role){
            ErrorMapUtils.putErrorMap(errorMap,"删除角色不存在");
            return errorMap;
        }
        List<JwUserRole> jwUserRoles = roleMapper.selectUserRoleByRoleId(id);
        if (jwUserRoles.size() > 0){
            ErrorMapUtils.putErrorMap(errorMap,"该角色存在用户正在使用，无法删除");
            return errorMap;
        }
        roleMapper.deleteRoleByid(id);
        return errorMap;
    }

    @Override
    public List<JwRole> findAllPowers() {
        List<JwRole> jwRoles = roleMapper.selectAll();
        return jwRoles;
    }

    /**
     * 修改role
     * @param role
     * @return
     */
    @Override
    public Map<String, String> modify(JwRole role) {
        roleMapper.updateByPrimaryKey(role);
        return null;
    }
}
